package com.gitee.sop.admin.controller.serve.notify;

import com.gitee.fastmybatis.core.PageInfo;
import com.gitee.fastmybatis.core.query.LambdaQuery;
import com.gitee.sop.admin.common.req.IdParam;
import com.gitee.sop.admin.common.resp.Result;
import com.gitee.sop.admin.common.util.CopyUtil;
import com.gitee.sop.admin.controller.serve.notify.param.NotifyInfoSearchParam;
import com.gitee.sop.admin.controller.serve.notify.param.NotifyPushParam;
import com.gitee.sop.admin.controller.serve.notify.vo.NotifyInfoVO;
import com.gitee.sop.admin.dao.entity.NotifyInfo;
import com.gitee.sop.admin.service.serve.NotifyInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author thc
 */
@RestController
@RequestMapping("serve/notify")
public class NotifyInfoController {

    @Autowired
    private NotifyInfoService notifyInfoService;

    /**
     * 分页查询
     *
     * @param param 查询参数
     * @return 返回分页结果
     */
    @GetMapping("/page")
    public Result<PageInfo<NotifyInfoVO>> page(NotifyInfoSearchParam param) {
        LambdaQuery<NotifyInfo> query = param.toLambdaQuery(NotifyInfo.class);
        PageInfo<NotifyInfoVO> pageInfo = notifyInfoService.doPage(query)
                .convert(data -> CopyUtil.copyBean(data, NotifyInfoVO::new));
        return Result.ok(pageInfo);
    }

    /**
     * 重新推送
     *
     * @param param 表单数据
     * @return 返回影响行数
     */
    @PostMapping("/push")
    public Result<Integer> update(@Validated @RequestBody NotifyPushParam param) {
        return Result.ok(notifyInfoService.push(param.getId(), param.getUrl()));
    }

    /**
     * 结束重试
     *
     * @param param 表单数据
     * @return 返回影响行数
     */
    @PostMapping("/end")
    public Result<Integer> end(@Validated @RequestBody IdParam param) {
        return Result.ok(notifyInfoService.end(param.getId()));
    }

}
